<?php

namespace App\Http\Controllers\Home;

use App\Http\Models\AnswerPaper;
use App\Http\Models\AnswerQuestion;
use App\Http\Models\Paper;
use App\Http\Models\Question;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ExamController extends Controller
{
    //试卷列表
    public function index(){
        $std_id = \Auth::guard('front')->user()->std_id;
        $info = Paper::with(['answerpaper'=>function($query) use ($std_id){
            $query->where('std_id',$std_id);
        }])
            ->get();
//        dd($info);
        return view('home.exam.index',compact('info'));
    }

    //试题展示
    public function run(Request $request,Paper $paper){
        //判断是否提交试卷
        if($request->isMethod('post')){
            $data = $request->all();
            //需要遍历接收的数据 接收的数据还有token_ 所有指定遍历的数组元素
            $data = $data['answer_'];
//            dd($data);
            //学生ID  需要将学生ID存储到表中
            $std_id = \Auth::guard('front')->user()->std_id;
            //总分
            $score_sum = '';
            //遍历数据入库(入库两种表 学生答案表answer_question（每一道小题回答的表） 学生试卷得分表answer_paper（试卷总分）)
            $arr['paper_id']      = $paper->paper_id;
            foreach($data as $key=>$val){
                $arr['std_id']        = $std_id;
                //$key 是每道题的ID
                $arr['qs_id']         = $key;
                $arr['answer_result'] = array_sum($data[$key]);
                //通过算法获取每道题的得分
                //首页先得到这道题的模型对象（通过该题的ID获取这条数据的对象（试题的ID则为$key 在模板中设置的））

                $question = Question::find($key);

                //通过question模型里面的getScore 方法 获取分数和是否正确
                $question_res = $question->getScore($arr['answer_result']);


                $arr['answer_score']  = $question_res['score'];
                $arr['right_wrong']   = $question_res['right'];
                //入库 存入answer_question表
                $ans_que = AnswerQuestion::create($arr);
                //求分数和
                $score_sum += $arr['answer_score'];
            }
            //入库 存入 学生得分表
            $arr['paper_score'] = $score_sum;
            $ans_pap = AnswerPaper::create($arr);
            if($ans_pap){
                echo '试卷提交成功';
                exit;
            }else{
                echo '试卷提交失败';
                exit;
            }
        }
        $dan_info = Question::where('paper_id',$paper->paper_id)
            ->where('qs_type','单选')
            ->get();
        $duo_info = Question::where('paper_id',$paper->paper_id)
            ->where('qs_type','多选')
            ->get();
        return view('home.exam.run',compact('paper','dan_info','duo_info'));
    }

    //查看试卷结果
    public function result(Request $request,Paper $paper){
        //试卷id
        $paper_id = $paper->paper_id;
        //学生id
        $std_id = \Auth::guard('front')->user()->std_id;
        //取出考试结果
        $info = AnswerPaper::where('paper_id',$paper_id)->first();
        //取出考题  考题又分为单选和多选 并通过渴求时加载 取出学员的答案
        //单选
        $single = Question::where('paper_id',$paper_id)
            ->with(['AnswerQuestion'=>function($q) use($std_id){
                $q->where('std_id',$std_id);
            }])
            ->where('qs_type','单选')
            ->get();
//        dd($single);
        //多选
        $multi = Question::where('paper_id',$paper_id)
            ->with(['AnswerQuestion'=>function($q) use($std_id){
                $q->where('std_id',$std_id);
            }])
            ->where('qs_type','多选')
            ->get();

        return view('home.exam.result',compact('paper','info','single','multi'));
    }
}
